/*
 * @Description: EEP检测 - 真实性检测
 * @Author: Rfan
 * @Date: 2022-10-10 14:19:09
 * @LastEditTime: 2022-10-21 18:11:26
 */

import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { Button, Checkbox, Space } from 'antd';
import cs from 'classnames';
import classnames from 'classnames/bind';
import { editItemById, insertItem, selectItemByKey } from '@/utils/list';
import { useAddPlanDialogContext } from '../../../contexts/AddPlanDialogContext';
import { CheckProps, EnumAuthenticityRuleType } from '../../../../../../dictionaries';
import type { IPropsRule } from '../../../../../../interface';
import styles from '../../../styles/entryPanes.module.scss';
import mainStyles from '../../../styles/main.module.scss';
import { isNullObj } from '@/utils';

const cx = classnames.bind(styles);
const mainCx = classnames.bind(mainStyles);
// EEP真实性规则列表
const EEPAuthList = [6, 7, 8];

// EEP真实性规则列表类型
type TRuleList = IPropsRule<EnumAuthenticityRuleType>[];

// EEP真实性完整类型
interface IAuthenticityFullRules {
  ruleType: CheckProps.Authenticity;
  rules: TRuleList;
}

const EEPAuthentication = (props: any, ref: any) => {
  const { currentRuleInfo, getPropertyRules, selectAllRules, selectInverseRules } =
    useAddPlanDialogContext();

  // 规则列表
  const [ruleList, setRuleList] = useState<TRuleList>([]);

  useImperativeHandle(ref, () => ({
    save: () => {
      const fullRules: IAuthenticityFullRules = {
        ruleType: CheckProps.Authenticity,
        rules: ruleList
      };
      return fullRules;
    }
  }));

  /**
   * @description: 获取到规则后回写
   */
  useEffect(() => {
    if (currentRuleInfo && !isNullObj(currentRuleInfo)) {
      const ruleObj = getPropertyRules(CheckProps.Authenticity);
      setRuleList(ruleObj?.rules || []);
    }
  }, [currentRuleInfo]);

  /**
   * @description: 获取规则选中状态
   * @param {EnumAuthenticityRuleType} type 真实性检测类型
   * @return {boolean} 选中状态
   */
  const getRuleState = (type: EnumAuthenticityRuleType): boolean => {
    const currentRule = selectItemByKey(type, ruleList, 'ruleType');
    return currentRule?.ruleValid;
  };

  /**
   * @description: 设置规则选中状态
   * @param {boolean} valid 选中状态
   * @param {EnumAuthenticityRuleType} type 真实性检测类型
   */
  const setRuleState = (valid: boolean, type: EnumAuthenticityRuleType) => {
    const currentRule = selectItemByKey(type, ruleList, 'ruleType');
    const editRule = currentRule
      ? editItemById(type, { ...currentRule, ruleValid: valid }, ruleList, 'ruleType')
      : insertItem({ ruleType: type, ruleValid: valid }, ruleList);
    setRuleList(editRule);
  };

  return (
    <div className={cs(mainCx('property-wrap'), cx('property-authenticity'))}>
      <Space className={mainCx('property--title-wrap')} align="center">
        <h6 className={mainCx('property--title')}>EEP格式包检测</h6>
        <div className={mainCx('select-btn')}>
          <Button
            type="text"
            size="small"
            onClick={() => {
              setRuleList(selectAllRules(EEPAuthList));
            }}
          >
            全选
          </Button>{' '}
          /{' '}
          <Button
            type="text"
            size="small"
            onClick={() => {
              setRuleList(selectInverseRules(EEPAuthList, ruleList));
            }}
          >
            反选
          </Button>
        </div>
      </Space>
      <Space direction="vertical">
        <Checkbox
          checked={getRuleState(EnumAuthenticityRuleType.EEPConsistency)}
          onChange={(evt) => {
            setRuleState(evt.target.checked, EnumAuthenticityRuleType.EEPConsistency);
          }}
        >
          采用数字摘要比对等方式对归档信息包的一致性进行检测
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumAuthenticityRuleType.EEPNormalization)}
          onChange={(evt) => {
            setRuleState(evt.target.checked, EnumAuthenticityRuleType.EEPNormalization);
          }}
        >
          电子档案封装包规范性检测(依据DA/T 48-2009附录B《电子文件封装包的Schema》进行检测)
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumAuthenticityRuleType.EEPSignValidity)}
          onChange={(evt) => {
            setRuleState(evt.target.checked, EnumAuthenticityRuleType.EEPSignValidity);
          }}
        >
          电子档案封装包电子签名有效性检测
        </Checkbox>
      </Space>
    </div>
  );
};

export default forwardRef(EEPAuthentication);
